#include "dlgstringedit.h"
#include "ui_dlgstringedit.h"

DlgStringEdit::DlgStringEdit(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgStringEdit)
{
    ui->setupUi(this);
    connect(ui->btn_ok, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->btn_cancel, SIGNAL(clicked()), this, SLOT(reject()));
    m_data_changed = false;
    connect(ui->lineEdit, SIGNAL(textEdited(QString)), this, SLOT(data_changed()));
    connect(ui->dateEdit, SIGNAL(dateChanged(QDate)), this,SLOT(data_changed()));
    connect(ui->btn_clear_date, SIGNAL(clicked()), this, SLOT(clear_date()));
    ui->dateEdit->hide();
    ui->btn_clear_date->hide();
    m_mode = 0;
    m_date_reset = false;
}

DlgStringEdit::~DlgStringEdit()
{
    delete ui;
}

void DlgStringEdit::set_data(const QString & qstr_edit, const QString & qstr_label, const QString &qstr_title)
{
    ui->lineEdit->setText(qstr_edit);
    ui->label->setText(qstr_label);
    this->setWindowTitle(qstr_title);
    QDate qd = QDate::fromString(qstr_edit,"dd.MM.yyyy");
    ui->dateEdit->setDate(qd);
}

QString DlgStringEdit::get_data()
{
   if(m_mode == 1)
   {
       if(m_date_reset) return "null";
       return ui->dateEdit->date().toString("dd.MM.yyyy");
   }
    return ui->lineEdit->text();
}

void DlgStringEdit::set_double_validator()
{
    mp_dval = new QDoubleValidator(this);
    ui->lineEdit->setValidator(mp_dval);
}

void DlgStringEdit::set_date_editor()
{
    ui->lineEdit->hide();
    ui->dateEdit->show();
    ui->btn_clear_date->show();
    m_mode = 1;
}

void DlgStringEdit::clear_date()
{
    m_date_reset = true;
    accept();
}

QDate   DlgStringEdit::get_date_data()
{
    if(m_date_reset) return QDate();
    return ui->dateEdit->date();
}
